using ERPBackend.Data;
using ERPBackend.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System;

namespace ERPBackend.Services
{
    public class WarehouseService : IWarehouseService
    {
        private readonly ApplicationDbContext _context;

        public WarehouseService(ApplicationDbContext context)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));
        }

        public async Task<IEnumerable<Warehouse>> GetAllWarehousesAsync()
        {
            return await _context.Warehouses.ToListAsync() ?? Enumerable.Empty<Warehouse>();
        }

        public async Task<int> GetTotalWarehousesAsync()
        {
            return await _context.Warehouses.CountAsync();
        }

        public async Task<IEnumerable<Warehouse>> GetWarehousesPagedAsync(int page, int pageSize)
        {
            int skip = (page - 1) * pageSize;
            return await _context.Warehouses
                .OrderBy(w => w.WarehouseCode)
                .Skip(skip)
                .Take(pageSize)
                .ToListAsync() ?? Enumerable.Empty<Warehouse>();
        }

        public async Task<Warehouse?> GetWarehouseByIdAsync(int id)
        {
            return await _context.Warehouses.FindAsync(id);
        }

        public async Task<Warehouse?> GetWarehouseByCodeAsync(string warehouseCode)
        {
            return await _context.Warehouses
                .FirstOrDefaultAsync(w => w.WarehouseCode == warehouseCode);
        }

        public async Task<Warehouse> CreateWarehouseAsync(Warehouse warehouse)
        {
            // 检查仓库编码是否已存在
            var existingWarehouse = await _context.Warehouses
                .FirstOrDefaultAsync(w => w.WarehouseCode == warehouse.WarehouseCode);
            if (existingWarehouse != null)
            {
                throw new ArgumentException("仓库编码已存在");
            }

            _context.Warehouses.Add(warehouse);
            await _context.SaveChangesAsync();
            return warehouse;
        }

        public async Task<bool> UpdateWarehouseAsync(Warehouse warehouse)
        {
            // 检查仓库是否存在
            var existingWarehouse = await _context.Warehouses.FindAsync(warehouse.Id);
            if (existingWarehouse == null)
            {
                return false;
            }

            // 检查仓库编码是否已被其他仓库使用
            if (existingWarehouse.WarehouseCode != warehouse.WarehouseCode &&
                await _context.Warehouses.AnyAsync(w => w.WarehouseCode == warehouse.WarehouseCode))
            {
                throw new ArgumentException("仓库编码已被使用");
            }

            // 更新属性
            existingWarehouse.WarehouseCode = warehouse.WarehouseCode;
            existingWarehouse.WarehouseName = warehouse.WarehouseName;
            existingWarehouse.Description = warehouse.Description;
            existingWarehouse.Address = warehouse.Address;
            existingWarehouse.Status = warehouse.Status;

            _context.Entry(existingWarehouse).State = EntityState.Modified;
            await _context.SaveChangesAsync();
            return true;
        }

        public async Task<bool> DeleteWarehouseAsync(int id)
        {
            var warehouse = await _context.Warehouses.FindAsync(id);
            if (warehouse == null)
            {
                return false;
            }

            _context.Warehouses.Remove(warehouse);
            await _context.SaveChangesAsync();
            return true;
        }

        public async Task<bool> ChangeWarehouseStatusAsync(int id, byte status)
        {
            var warehouse = await _context.Warehouses.FindAsync(id);
            if (warehouse == null)
            {
                return false;
            }

            warehouse.Status = status;
            _context.Entry(warehouse).State = EntityState.Modified;
            await _context.SaveChangesAsync();
            return true;
        }
    }
}